package xyz.ranyh.jobclient.util;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import xyz.ranyh.jobclient.R;
import xyz.ranyh.jobclient.http.Resource;
import xyz.ranyh.jobclient.http.StarJobTask;
import xyz.ranyh.jobclient.http.Url;

/**
 * Created by zhaoshengping on 2016/12/28.
 */

public class JobsListAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<JobBean> jobs;
    private ViewClickListener listener;
    private static String TAG = "JobsListAdapter";

    public void setListener(ViewClickListener listener) {
        this.listener = listener;
    }

    public interface ViewClickListener {
        void onClick(int i, View view, String url);

        void loadmoreListener();

        void hideLoadmore();
    }

    public JobsListAdapter(Context context, ArrayList<JobBean> jobs) {
        this.context = context;
        this.jobs = jobs;
    }

    public ArrayList<JobBean> getJobs() {
        return jobs;
    }

    public void setJobs(ArrayList<JobBean> jobs) {
        this.jobs = jobs;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return jobs.size();
    }

    @Override
    public JobBean getItem(int i) {
        return jobs.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    public void removeAll() {
        Integer len = jobs.size();
        for (int i = len - 1; i >= 0; i--) {
            jobs.remove(i);
        }
        notifyDataSetChanged();
    }
    public void add (JobBean job) {
        jobs.add(job);
        notifyDataSetChanged();
    }

    private class ViewHolder {
        TextView mTitleView;
        TextView mPublishedDateView;
        TextView mTagsView;
        TextView mStarsView;
        TextView mOpenDateView;
        Button loadMoreView;
        ImageButton mStarButton;
        ImageButton mAlarmButton;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        final JobBean item = getItem(i);
//        Log.d(TAG, "getView: " + String.valueOf(i) + " of" + String.valueOf(jobs.size()) + item.getRefUrl());
        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.home_list, viewGroup, false);
            holder.mTitleView = (TextView) view.findViewById(R.id.jobListTitle);
            holder.mPublishedDateView = (TextView) view.findViewById(R.id.jobListTime);
            holder.mTagsView = (TextView) view.findViewById(R.id.tags_view);
            holder.mStarsView = (TextView) view.findViewById(R.id.stars_view);
            holder.mOpenDateView = (TextView) view.findViewById(R.id.open_date_view);
            holder.mStarButton = (ImageButton) view.findViewById(R.id.star_button);
            holder.mAlarmButton = (ImageButton) view.findViewById(R.id.alarm_button);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();

        }

        if (i == jobs.size() - 1) {
//            Log.d(TAG, "getView: loadmore");
            listener.loadmoreListener();
        } else {
            listener.hideLoadmore();
        }
        holder.mTitleView.setText(item.getTitle());
        holder.mPublishedDateView.setText(item.getPublishedDate());
        holder.mTagsView.setText(item.getTagsString());
        holder.mStarsView.setText(String.valueOf(item.getFollowerCount()));
        holder.mOpenDateView.setText(item.getOpenDate());

        holder.mStarButton.setImageResource(R.drawable.star_off);
        if (item.getStar() == null) {
            item.setStar(false);
            List<Integer> userStar = Resource.getInstance().getUser().getStar();
            for (int j : userStar) {
                if (j == item.getId()) {
                    holder.mStarButton.setImageResource(R.drawable.star);
                    item.setStar(true);
                    break;
                }
            }
        } else if (item.getStar()) {
            holder.mStarButton.setImageResource(R.drawable.star);
        }

        final ViewHolder FH = holder;
        holder.mStarButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url;
                if (getItem(i).getStar()) {
                    url = Url.UNSTAR_JOB_URL;
                } else {
                    url = Url.STAR_JOB_URL;
                }
                new StarJobTask(getItem(i).getId(), url) {
                    @Override
                    protected void onPostExecute(final JSONObject success) {
                        if (success != null) {
                            try {
                                Toast.makeText(context, success.getString("message"), Toast.LENGTH_SHORT).show();
                                if (item.getStar()) {
                                    FH.mStarButton.setImageResource(R.drawable.star_off);
                                    getItem(i).setStar(false);
                                    Resource.getInstance().getUser().getStar().remove((Integer)item.getId());
                                } else {
                                    FH.mStarButton.setImageResource(R.drawable.star);
                                    getItem(i).setStar(true);
                                    Resource.getInstance().getUser().getStar().add(getItem(i).getId());
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                }.execute();
            }
        });
//        }

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Log.d("view on click", "" + i);
                String url = jobs.get(i).getRefUrl();
                listener.onClick(i, view, url);
            }
        });
        return view;
    }
}
